package com.supinfo.youfood.forms;

import java.rmi.RemoteException;

import javax.xml.rpc.ServiceException;

import org.apache.log4j.Logger;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.IModel;

import com.supinfo.food.entities.User;
import com.supinfo.food.services.UserService;
import com.supinfo.food.services.UserServiceService;
import com.supinfo.food.services.UserServiceServiceLocator;

/**
 * Formulaire d'ajout d'utilisateur
 * @author Keitaro
 */
@SuppressWarnings("rawtypes")
public class AddUserForm extends Form {

	private static final long serialVersionUID = 1L;
	private Logger logger = Logger.getLogger(getClass());
	
	@SuppressWarnings("unchecked")
	public AddUserForm(String id, IModel model) {
		super(id, model);
		
		TextField login = new TextField("login");
		login.setRequired(true);
		add(login);
		
		add(new PasswordTextField("password"));
	}
	
	@Override
	protected void onSubmit() {
		User user = (User) getModelObject();
		boolean result = false;
		
		try {
			UserServiceService binding = new UserServiceServiceLocator();
			UserService service = binding.getUserService();
			result = service.add(user);
		} catch (ServiceException e) {
			logger.info("Service can not be reached.");
		} catch (RemoteException e) {
			logger.info("Service does not responded.");
		}
		
		if (result == false) {
			info("User can not be created.");
		} else {
			info("User has been created.");
		}
	}

}
